﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using ParkingLot.Utility.SerializeHelp;

namespace ParkingLot.Utility.CollectionEntity
{
    /// <summary>
    /// 集合的保存和读取
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class CollectionSRHelp<T>
    {
        private static readonly object m_LockXmlFile = new object();

        /// <summary>
        /// 是否完成设置路径
        /// </summary>
        public static bool HaveSetPath { private set; get; } = false;

        private static string m_Folder = "";

        private static string m_FileName = "";

        public List<T> LisT = new List<T>();

        /// <summary>
        /// 设置保存的路径
        /// </summary>
        /// <param name="folder"></param>
        /// <param name="fileName"></param>
        public static void SetSavePath(string folder,string fileName)
        {
            m_Folder = folder;
            m_FileName = fileName;
            HaveSetPath = true;
        }

        /// <summary>
        /// 保存本节点(注意调用本方法前需要调用SetSavePath 而且T必须为可以串行化的)
        /// </summary>
        /// <param name="lisencePlateInfo"></param>
        public static void Save(T t,out string error)
        {
            error = "";
            if (!HaveSetPath)
            {
                error = "没有设置文件路径";
                return;
            }
            string todayFolder = $"{m_Folder}\\{DateTime.Now.ToString("yyyyMMdd")}";
            if (!Directory.Exists(todayFolder))
            {
                Directory.CreateDirectory(todayFolder);
            }

            string todayPath = $"{todayFolder}\\{m_FileName}";
            CollectionSRHelp<T> collectionSrHelp;
            lock (m_LockXmlFile)
            {
                if (!File.Exists(todayPath))
                {
                    collectionSrHelp = new CollectionSRHelp<T>();
                }
                else
                {
                    XDocument xdoc = XDocument.Load(todayPath);
                    collectionSrHelp = XmlHelp.DeXMLSerialize<CollectionSRHelp<T>>(xdoc.ToString());
                }

                collectionSrHelp.LisT.Add(t);
                string value = XmlHelp.ToString(collectionSrHelp, collectionSrHelp.GetType(),
                    Encoding.UTF8);
                XDocument xdocIndex = XDocument.Parse(value);
                xdocIndex.Save(todayPath);
            }
        }

        /// <summary>
        /// 保存本节点(注意调用本方法前需要调用SetSavePath 而且T必须为可以串行化的)
        /// </summary>
        /// <param name="lisencePlateInfo"></param>
        public static void Save(T t,DateTime dt, out string error)
        {
            error = "";
            if (!HaveSetPath)
            {
                error = "没有设置文件路径";
                return;
            }
            string todayFolder = $"{m_Folder}\\{dt.ToString("yyyyMMdd")}";
            if (!Directory.Exists(todayFolder))
            {
                Directory.CreateDirectory(todayFolder);
            }

            string todayPath = $"{todayFolder}\\{m_FileName}";
            CollectionSRHelp<T> collectionSrHelp;
            lock (m_LockXmlFile)
            {
                if (!File.Exists(todayPath))
                {
                    collectionSrHelp = new CollectionSRHelp<T>();
                }
                else
                {
                    XDocument xdoc = XDocument.Load(todayPath);
                    collectionSrHelp = XmlHelp.DeXMLSerialize<CollectionSRHelp<T>>(xdoc.ToString());
                }

                collectionSrHelp.LisT.Add(t);
                string value = XmlHelp.ToString(collectionSrHelp, collectionSrHelp.GetType(),
                    Encoding.UTF8);
                XDocument xdocIndex = XDocument.Parse(value);
                xdocIndex.Save(todayPath);
            }
        }

        /// <summary>
        /// 获取历史记录
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static List<T> GEtCornerPlcEntities(string path)
        {
            XDocument xdoc = XDocument.Load(path);
            var m_Instance = XmlHelp.DeXMLSerialize<CollectionSRHelp<T>>(xdoc.ToString());
            return m_Instance.LisT;
        }
    }
}
